package com.kzj.mls.activity;

import android.os.Bundle;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.kzj.mls.R;
import com.kzj.mls.beans.ServiceBookBean;
import com.kzj.mls.global.App;
import com.kzj.mls.global.Constant;
import com.kzj.mls.recyclerCard.basic.MaterialListView;
import com.kzj.mls.recyclerCard.basic.OnButtonPressListener;
import com.kzj.mls.recyclerCard.card.Card;
import com.kzj.mls.recyclerCard.card.other.EditAddressCard;
import com.kzj.mls.request.BaseResponse;
import com.kzj.mls.request.ICallBackForRequestServer;
import com.kzj.mls.request.RequestServerManager;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by xyg on 2015/11/3.
 */
public class EditAddressActivity extends GBaseActivity {
    private MaterialListView material_listview;
    private ServiceBookBean.HomeAddressEntity addressBean;
    private EditAddressCard editAddressCard;
    private ServiceBookBean serviceBookBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentViewFor720_1280(R.layout.template_materlistview);
        findViewByIds();
        initDatas();
        fillArrayInListView();
    }

    @Override
    protected void findViewByIds() {
        material_listview = (MaterialListView) findViewById(R.id.material_listview);
    }

    @Override
    protected void initDatas() {
        setTopTitle("编辑上门服务地址");
        setRightText(View.VISIBLE, "存储");
        setLeftImg(View.VISIBLE, R.drawable.back);
        addressBean = (ServiceBookBean.HomeAddressEntity) getIntent().getSerializableExtra("address_bean");
        serviceBookBean = (ServiceBookBean) getIntent().getSerializableExtra("beans");
    }

    @Override
    protected void fillArrayInListView() {
        editAddressCard = new EditAddressCard(this);
        editAddressCard.setAddressBean(addressBean);
        editAddressCard.setOnNormalButtonPressedListener(new OnButtonPressListener() {
            @Override
            public void onButtonPressedListener(View view, Card card) {
                TaskDeleteAddress(addressBean.getAddr_id());
            }
        });
        if (addressBean != null)
            editAddressCard.setIsShowDelete(true);
        material_listview.add(editAddressCard);
    }


    private void TaskEditAddress(final String addr_id, final String address) {
        RequestServerManager.getInstance().handleMethod((GBaseActivity) getActivity(), null, null, false,
                Constant.getRootUrl() + Constant.Action.Action_Edit_Address, RequestServerManager.getInstance().
                        getRequestParams(Constant.Action.Action_Edit_Address, addr_id, address), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        List<ServiceBookBean.HomeAddressEntity> addressBeans = JSON.parseArray(result.getRetval().toString(), ServiceBookBean.HomeAddressEntity.class);
                        if (serviceBookBean != null) {
                            serviceBookBean.setAddr(addressBeans);
                            EventBus.getDefault().post(serviceBookBean);
                        } else {
                            EventBus.getDefault().post(addressBeans);
                        }
                        EditAddressActivity.this.finish();
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {
                        App.getInstance().showToast(errorMsg + "");
                    }
                });
    }

    private void TaskAddAddress(final String address, String region_name) {
        RequestServerManager.getInstance().handleMethod((GBaseActivity) getActivity(), null, null, false,
                Constant.getRootUrl() + Constant.Action.Action_Add_Address, RequestServerManager.getInstance().
                        getRequestParams(Constant.Action.Action_Add_Address, address, region_name), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        List<ServiceBookBean.HomeAddressEntity> addressBeans = JSON.parseArray(result.getRetval().toString(), ServiceBookBean.HomeAddressEntity.class);
                        if (serviceBookBean != null) {
                            serviceBookBean.setAddr(addressBeans);
                            EventBus.getDefault().post(serviceBookBean);
                            EditAddressActivity.this.finish();
                        } else {
                            EventBus.getDefault().post(addressBeans);
                            EditAddressActivity.this.finish();
                        }
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {
                        App.getInstance().showToast(errorMsg + "");
                    }
                });
        ;
    }

    private void TaskDeleteAddress(final String addr_id) {
        RequestServerManager.getInstance().handleMethod((GBaseActivity) getActivity(), null, null, false,
                Constant.getRootUrl() + Constant.Action.Action_Delete_Address, RequestServerManager.getInstance().
                        getRequestParams(Constant.Action.Action_Delete_Address, addr_id), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        List<ServiceBookBean.HomeAddressEntity> addressBeans = JSON.parseArray(result.getRetval().toString(), ServiceBookBean.HomeAddressEntity.class);
                        if (serviceBookBean != null) {
                            serviceBookBean.setAddr(addressBeans);
                            EventBus.getDefault().post(serviceBookBean);
                        } else {
                            EventBus.getDefault().post(addressBeans);
                        }
                        EditAddressActivity.this.finish();
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {
                        App.getInstance().showToast(errorMsg + "");
                    }
                });
    }

    @Override
    public void doClickRightText() {
        super.doClickRightText();
        if (addressBean != null) {
            TaskEditAddress(addressBean.getAddr_id(), editAddressCard.getAddressBean().getAddress());
        } else {
            TaskAddAddress(editAddressCard.getAddress(), App.getInstance().getCity());
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
    }
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getSimpleName()); //统计页面(仅有Activity的应用中SDK自动调用，不需要单独写)
        MobclickAgent.onResume(this);          //统计时长
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getSimpleName()); // （仅有Activity的应用中SDK自动调用，不需要单独写）保证 onPageEnd 在onPause 之前调用,因为 onPause 中会保存信息
        MobclickAgent.onPause(this);
    }
}
